package com.example.aop.proxy;

import org.springframework.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

public class Main {
    public static void main(String[] args) {
//        HouseSubject houseProxy = new HouseProxy(new RealHouseSubject());
//        houseProxy.rent();
//
//        System.out.println("=============================");
//        HouseSubject houseSubject = new RealHouseSubject();
//        houseSubject.rent();




        //JDK动态代理
        //代理接口
//        RealHouseSubject target = new RealHouseSubject();
//        HouseSubject proxy = (HouseSubject)  Proxy.newProxyInstance(target.getClass().getClassLoader(),
//                new Class[]{HouseSubject.class},
//                new JDKInvocationHandler(target));
//        proxy.rent();
//        proxy.sell();


        //不能代理类
//        RealHouseSubject target = new RealHouseSubject();
//        RealHouseSubject proxy = (RealHouseSubject)  Proxy.newProxyInstance(target.getClass().getClassLoader(),
//                new Class[]{RealHouseSubject.class},
//                new JDKInvocationHandler(target));
//        proxy.rent();
//        proxy.sell();



        //使用 CGLIB 完成代理
        HouseSubject target = new RealHouseSubject();
        //代理接口
//        HouseSubject houseSubject = (HouseSubject)  Enhancer.create(target.getClass(),new CGLIBMethodInterceptor(target));
//        houseSubject.rent();
//        houseSubject.sell();

        //代理类
        RealHouseSubject houseSubject = (RealHouseSubject)  Enhancer.create(target.getClass(),new CGLIBMethodInterceptor(target));
        houseSubject.rent();
        houseSubject.sell();
    }
}
